package com.aibaixun.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.aibaixun.common.constant.ApiConstants;
import com.aibaixun.common.utils.HttpUtils;
import com.aibaixun.common.utils.StringUtils;
import com.aibaixun.system.domain.dto.DicmAddressData;
import com.aibaixun.system.mapper.DicmAddressDataMapper;
import com.aibaixun.system.service.DicmAddressDataService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <b>DicmAddressDataServiceImpl</b>
 *
 * @description: DicmAddressDataServiceImpl <br>
 * @date: 2024/8/9 17:05 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Service
public class DicmAddressDataServiceImpl extends ServiceImpl<DicmAddressDataMapper, DicmAddressData> implements DicmAddressDataService {
    private static final Logger logger = LoggerFactory.getLogger(DicmAddressDataServiceImpl.class);


    @Override
    public List<DicmAddressData> queryEnergyInfo() {
        String date = DateUtil.format(new Date(), "yyyy-MM-dd HH");
        List<DicmAddressData> dicmAddressDataList = baseMapper.queryEnergyInfo(date);
        if (CollectionUtil.isNotEmpty(dicmAddressDataList)){
            JSONObject jsonObject = HttpUtils.sendGetRequest(ApiConstants.IP_ADDRESS
                +ApiConstants.BLADE_ENERGY);
            if (null != jsonObject){
                Map<String,String> map = jsonObject.toJavaObject(Map.class);
                if (CollectionUtil.isNotEmpty(map)){
                    map.forEach((key,value) -> {
                        DicmAddressData data = new DicmAddressData();
                        data.setAddressName(key);
                        data.setAddressNumber(value.toString());
                        data.setCreateTime(new Date());
                        dicmAddressDataList.add(data);
                    });
                }
            }
        }
        return dicmAddressDataList;
    }

    @Override
    public List<DicmAddressData> queryEnergyInfoDate(String date) {
        return baseMapper.queryEnergyInfo(date);
    }
}
